package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.Collections;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.jackysoft.entity.FieldAccess;
import org.jackysoft.entity.FieldAccessPK;
import org.jackysoft.service.AbstractServiceImpl;
import org.jackysoft.service.FieldAccessService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FieldAccessServiceImpl extends AbstractServiceImpl<FieldAccessPK,FieldAccess>
		implements FieldAccessService {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7267295647565019127L;

	
	
	public FieldAccessServiceImpl() {
		this.type = FieldAccess.class;
	}

	@Override
	public Collection<FieldAccess> get(FieldAccess f)
			throws DataAccessException {
		return super.getsByExample(f);
	}

	@Override
	public void preProceed(FieldAccess f) throws DataAccessException {
		FieldAccess tf =  get(f.getId());
		if(tf==null){
			create(f);
		}else{
		    tf.getReadonly().clear();
		    tf.getReadonly().addAll(f.getReadonly());
			update(tf);
		}
		
	}

	@Override
	public Collection<String> getReadonly(FieldAccessPK id)
			throws DataAccessException {
		FieldAccess fa = get(id);
		if(fa==null)
			return Collections.emptyList();
		return fa.getReadonly();
	}

	@Override
	@Resource(name = "idtSessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		// TODO Auto-generated method stub
		super.setSessionFactory(sessionFactory);
	}
	
	
	
}